import axios from 'axios'
import querystring from 'querystring'
import Config from '@/config/index'
export const http = axios.create({
  baseURL: Config.baseUrl,
  timeout: 2000,
  // withCredentials: true,
  headers: {'X-Requested-With': 'XMLHttpRequest'},
  transformRequest: [(data, headers) => {
    headers['Accept'] = 'application/json'
    /**
     * 说明要上传文件的时候
     */
    if (headers['Content-Type'] === 'multipart/form-data') {
      return data
    }
    /**
     * 需要转成JSON的形式，Spring MVC才能从RequestBody中反序列化对象
     */
    if (headers['Content-Type'] === 'application/json') {
      return JSON.stringify(data)
    }
    /**
     * FormData的时候，需要转化数据
     */
    return querystring.stringify(data)
  }]
})
